package cn.whuc.homework

import scala.collection.mutable.ArrayBuffer

object Case05_1 {
  def main(args: Array[String]): Unit = {
    val list: List[Array[String]] =
      scala.io.Source.fromFile("input/primary_midsemester.txt").getLines().drop(1).toList.map(_.split(" "))
      fun1(list)
      fun2(list)

  }
  def fun1(list:List[Array[String]]): Unit ={
    val chinese: ArrayBuffer[Int] = new ArrayBuffer[Int](list.size - 1)
    val english: ArrayBuffer[Int] = new ArrayBuffer[Int](list.size - 1)
    val math: ArrayBuffer[Int] = new ArrayBuffer[Int](list.size - 1)

    for (elem <- list) {
      val strings: Array[String] = elem.takeRight(3)
      chinese+=strings(0).toInt
      english+=strings(1).toInt
      math+=strings(2).toInt
    }

    println("语文平均分：" + chinese.sum / chinese.size.toDouble + " 最高分：" + chinese.max + " 最低分：" + chinese.min)
    println("英语平均分：" + english.sum / english.size.toDouble + " 最高分：" + english.max + " 最低分：" + english.min)
    println("数学平均分：" + math.sum / math.size.toDouble + " 最高分：" + math.max + " 最低分：" + math.min)
  }

  def fun2(list:List[Array[String]])={
    val tuples: List[(String, Double)] = list.map(arr => {
      (arr(0), arr.takeRight(3).map((_.toDouble)).sum/3 )
    })

    println("学号，均分逆序：")
    println(tuples.sortBy(_._2).reverse)
  }

}
